package com.wxj.edu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxj.commonutils.result.R;
import com.wxj.edu.entity.Teacher;
import com.wxj.edu.entity.vo.TeacherQuery;
import com.wxj.edu.service.CourseService;
import com.wxj.edu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲师 前端控制器
 *
 * @author wxj
 * @since 2021-03-02
 */
@RestController
@RequestMapping("/edu/teacher")
//@CrossOrigin
public class TeacherController {

    //把service注入
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    /**
     * 1、查询讲师表所有数据
     */
    @GetMapping("findAll")
    public R findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<Teacher> list = teacherService.list(null);
        return R.success().data("items", list);
    }

    /**
     * 2、逻辑删除讲师方法
     */
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id) {

        boolean flag = courseService.findTeacherId(id);
        if (flag) {
            teacherService.removeById(id);
            return R.success();
        } else {
            return R.error().message("讲师已发布课程，不可删除！");
        }
    }

    /**
     * 3、分页查询讲师的方法
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {
        //创建 page 对象
        Page<Teacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到 pageTeacher 对象里面
        teacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();//总记录数
        List<Teacher> records = pageTeacher.getRecords();   //数据list集合
        return R.success().data("total", total).data("rows", records);
    }

    /**
     * 4、条件查询带分页的方法
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //多条件查询
        //mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<Teacher> records = pageTeacher.getRecords();   //数据list集合
        return R.success().data("total", total).data("rows", records);
    }

    /**
     * 5、添加讲师接口的方法
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.success();
        } else {
            return R.error();
        }
    }

    /**
     * 6、根据讲师id进行查询
     */
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.success().data("teacher",teacher);
    }

    /**
     * 7、讲师修改功能
     */
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return R.success();
        } else {
            return R.error();
        }
    }

}

